{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d69e7a3b",
   "metadata": {},
   "source": [
    "## 实践：\n",
    "> 1. 创建faceset\n",
    "> 2. 查询faceset\n",
    "> 3. 存入face_token数据\n",
    "> 4. 获取faceset信息数据\n",
    "> 5. 人脸搜索\n",
    "> 6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89c3c0e8",
   "metadata": {},
   "source": [
    "### 1.创建faceset\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "daf31ad1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-0 准备工作\n",
    "import requests\n",
    "API_KEY = \"N4jJQNtBkzNgVcOqZV-P34mKhYPm0hk2\"\n",
    "API_SERCRET = \"nNhPtXGnphFeKs0o7E_tALIoyuQ2hh7e\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "03ee492d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-1 创建人脸数据集合\n",
    "FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\":API_KEY,\n",
    "    \"api_sercret\":API_SERCRET\n",
    "}\n",
    "r = requests.post(FaceSet_url,params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "706f742b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d4b8651d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e40431b403debd7c78bfab160d3ac2e9'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()[\"faceset_token\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3e9898fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e40431b403debd7c78bfab160d3ac2e9'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()[\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fd2de99",
   "metadata": {},
   "source": [
    "### 2.查询FaceSet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d1c3448c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# B-1 检查是否创建成功\n",
    "getdetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    \"api_key\":API_KEY,\n",
    "    \"api_sercret\":API_SERCRET,\n",
    "    \"faceset_token\":faceset_token\n",
    "}\n",
    "r = requests.post(getdetail_url,params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1e722589",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3c590bd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e40431b403debd7c78bfab160d3ac2e9',\n",
       " 'tags': '',\n",
       " 'time_used': 89,\n",
       " 'user_data': '59人',\n",
       " 'display_name': 'API第四周',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1647919013,e30ce842-f87b-4872-9088-4f77c270888a',\n",
       " 'outer_id': 'APIweek04'}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24cda378",
   "metadata": {},
   "source": [
    "### 3.更新人脸库信息 API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "58b3223a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# C-1\n",
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    \"api_key\":API_KEY,\n",
    "    \"api_sercret\":API_SERCRET,\n",
    "    \"faceset_token\":faceset_token,\n",
    "    \"new_outer_id\":\"APIweek04\",\n",
    "    \"display_name\":\"API第四周\",\n",
    "    \"user_data\":\"59人\",\n",
    "    \"tags\":\"\"\n",
    "}\n",
    "r = requests.post(update_url,params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3879531f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "85346c6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'e40431b403debd7c78bfab160d3ac2e9',\n",
       " 'request_id': '1647918972,f4f21382-b94e-479d-ad39-ccfa2264259b',\n",
       " 'time_used': 72,\n",
       " 'outer_id': 'APIweek04'}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7517d0a9",
   "metadata": {},
   "source": [
    "### 4.获取人脸库列表及信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1056a863",
   "metadata": {},
   "outputs": [],
   "source": [
    "# D-1\n",
    "getfacesets_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "payload = {\n",
    "    \"api_key\":API_KEY,\n",
    "    \"api_sercret\":API_SERCRET\n",
    "}\n",
    "r = requests.post(getfacesets_url,params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6b3962d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "5e5df3fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 73,\n",
       " 'facesets': [{'faceset_token': '3f7560c6df519fdd523939dc92682805',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'e40431b403debd7c78bfab160d3ac2e9',\n",
       "   'outer_id': 'APIweek04',\n",
       "   'display_name': 'API第四周',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647919864,bda119b5-5a1a-4963-a31d-f2b09375c9fd'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5712693a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "7b596841",
   "metadata": {},
   "source": [
    "## 2.实现所有人脸功能的应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b2cd961c",
   "metadata": {},
   "source": [
    "### 1.实现人脸检测获取face_token\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f9d52aee",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "401e909f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5fc68eb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "196ce75d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648368523,418e16da-bcdf-47ad-a3ef-78fdad5b55ed',\n",
       " 'time_used': 478,\n",
       " 'faces': [{'face_token': '4db91fdd4dca057048e998d611cfc15e',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_KEY,API_SERCRET,\"Liu_01.jpeg\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49781d17",
   "metadata": {},
   "source": [
    "## 3.本周作业\n",
    "* 1. 尝试创建detect.py 并实现功能调用\n",
    "* 2. 尝试创建analyze.py/campare.py/search.py,并实现功能调用\n",
    "* 3. 尝试存放宿舍四位同学的人脸信息到 人脸数据库（xxx栋xxx宿舍）：\n",
    "> 1. 完善faceset信息\n",
    "> 2. 实现人脸搜索和人脸对比，打印结果：是本人OR不是本人\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "095e7ef8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
